package com.project.controller;

import com.project.entity.UserEntity;
import com.project.token.TokenSys;
import com.project.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;



@Slf4j
@RestController
@RequestMapping("boot")
@Api(tags = "上传接口")

public class UploadController {

    //get post put delete
   @PostMapping("upload")
   @ApiOperation(value = "文件上传")
    public String upload(@RequestParam("file")MultipartFile file) throws IOException {
         //判断格式 --防止黑客攻击
          //重命名--->防止覆盖
           //判断文件是否存在
       String os = System.getProperty("os.name");
       File f=null;
       if(os != null && os.toLowerCase().startsWith("windows")){
           f = new File(FileUtil.W_DIR+"/"+file.getOriginalFilename());
       }else {
           f = new File(FileUtil.L_DIR+"/"+file.getOriginalFilename());
       }
       file.transferTo(f);
        return file.getOriginalFilename();
    }


    @ApiOperation(value = "aop测试")
    @GetMapping("/test")
    public String test(){
       return "hello";
    }

    @ApiOperation(value = "用户信息")
    @GetMapping("userInfo/{userName}/{pwd}/info")
    public String userInfo(@PathVariable("userName") String userName,
                           @PathVariable("pwd") String pwd,
                           HttpServletRequest request){
//        String path=  request.getServletPath();
//        log.info(path);
//        //验证
//        String  token=request.getHeader("token");
//        if(TokenSys.verifierJwt(token).equals("502")){
//            return "";
//        }
       String user = "";
       if (userName.equals("li")){
            user = "李四";
       } else if (userName.equals("zy")) {
           user = "赵三强";
       }else if (userName.equals("")){
           user = "错误";
       }
       return user;
    }


    @PostMapping("userSave")
    @ApiOperation(value = "保存用户")
    public String userSave(UserEntity userEntity){
//        System.out.println(userEntity.getName());
        log.info(userEntity.getUserName());
        return "ok";
    }



}
